package usecase

import (
	"Data-API-MCP/backend/internal/application/dto"
	"Data-API-MCP/backend/internal/domain/service"
	"context"
	"time"
)

// UserUseCase 用户用例
type UserUseCase struct {
	userService service.UserService
}

// NewUserUseCase 创建用户用例
func NewUserUseCase(userService service.UserService) *UserUseCase {
	return &UserUseCase{
		userService: userService,
	}
}

// Register 用户注册
func (uc *UserUseCase) Register(ctx context.Context, req *dto.UserRegisterRequest) (*dto.UserInfoResponse, error) {
	user, err := uc.userService.Register(ctx, req.Username, req.Password, req.Email)
	if err != nil {
		return nil, err
	}

	return &dto.UserInfoResponse{
		ID:        user.ID,
		Username:  user.Username,
		Email:     user.Email,
		Phone:     user.Phone,
		Status:    user.Status,
		CreatedAt: user.CreatedAt.Format(time.RFC3339),
		UpdatedAt: user.UpdatedAt.Format(time.RFC3339),
	}, nil
}

// Login 用户登录
func (uc *UserUseCase) Login(ctx context.Context, req *dto.UserLoginRequest) (*dto.UserLoginResponse, error) {
	token, err := uc.userService.Login(ctx, req.Username, req.Password)
	if err != nil {
		return nil, err
	}

	return &dto.UserLoginResponse{
		Token: token,
	}, nil
}

// GetUserInfo 获取用户信息
func (uc *UserUseCase) GetUserInfo(ctx context.Context, id uint) (*dto.UserInfoResponse, error) {
	user, err := uc.userService.GetUserInfo(ctx, id)
	if err != nil {
		return nil, err
	}

	return &dto.UserInfoResponse{
		ID:        user.ID,                             // 用户ID
		Username:  user.Username,                       // 用户名
		Email:     user.Email,                          // 邮箱
		Phone:     user.Phone,                          // 手机号
		Status:    user.Status,                         // 状态
		CreatedAt: user.CreatedAt.Format(time.RFC3339), // 创建时间
		UpdatedAt: user.UpdatedAt.Format(time.RFC3339), // 更新时间
	}, nil
}

// UpdateUserInfo 更新用户信息
func (uc *UserUseCase) UpdateUserInfo(ctx context.Context, id uint, req *dto.UserUpdateRequest) error {
	user, err := uc.userService.GetUserInfo(ctx, id)
	if err != nil {
		return err
	}

	if req.Email != "" {
		user.Email = req.Email
	}
	if req.Phone != "" {
		user.Phone = req.Phone
	}

	return uc.userService.UpdateUserInfo(ctx, user)
}

// ChangePassword 修改密码
func (uc *UserUseCase) ChangePassword(ctx context.Context, id uint, req *dto.UserChangePasswordRequest) error {
	return uc.userService.ChangePassword(ctx, id, req.OldPassword, req.NewPassword)
}

// ListUsers 获取用户列表
func (uc *UserUseCase) ListUsers(ctx context.Context, offset, limit int) (*dto.UserListResponse, error) {
	users, total, err := uc.userService.ListUsers(ctx, offset, limit)
	if err != nil {
		return nil, err
	}

	items := make([]dto.UserInfoResponse, len(users))
	for i, user := range users {
		items[i] = dto.UserInfoResponse{
			ID:        user.ID,                             // 用户ID
			Username:  user.Username,                       // 用户名
			Email:     user.Email,                          // 邮箱
			Phone:     user.Phone,                          // 手机号
			Status:    user.Status,                         // 状态
			CreatedAt: user.CreatedAt.Format(time.RFC3339), // 创建时间
			UpdatedAt: user.UpdatedAt.Format(time.RFC3339), // 更新时间
		}
	}

	return &dto.UserListResponse{
		Total: total, // 总条数
		Items: items, // 用户列表
	}, nil
}

// DeleteUser 删除用户
func (uc *UserUseCase) DeleteUser(ctx context.Context, id uint) error {
	return uc.userService.DeleteUser(ctx, id)
}
